﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using SlamDunk.MES.IAppService.DTOs.Outputs.QualityContext;
using SlamDunk.MES.IAppService.Interfaces;

namespace SlamDunk.MES.Client.ViewModels.ExecutionPlanInspection
{
    /// <summary>
    /// 审核成品质检ViewModel
    /// </summary>
    public class CheckViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 质量管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IQualityContract> _iQualityContract;

        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 编号生成服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<INumberContract> _iNumberContract;
        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public CheckViewModel(ServiceProxy<IQualityContract> iQualityContract, ServiceProxy<IExecutionContract> iExecutionContract, ServiceProxy<INumberContract> iNumberContract, IWindowManager windowManager)
        {
            this._iQualityContract = iQualityContract;
            this._iExecutionContract = iExecutionContract;
            this._iNumberContract = iNumberContract;
            this._windowManager = windowManager;
        }

        #endregion

        #region 属性
        #region 质量检查编号
        /// <summary>
        /// 质量检查编号
        /// </summary>
        [DependencyProperty]
        public string InspectionNo { get; set; }
        #endregion

        #region 质量检查名称
        /// <summary>
        /// 质量检查名称
        /// </summary>
        [DependencyProperty]
        public string InspectionName { get; set; }
        #endregion

        #region 质检日期
        /// <summary>
        /// 质检日期
        /// </summary>
        [DependencyProperty]
        public DateTime? InspectedDate { get; set; }
        #endregion

        #region 作业计划
        /// <summary>
        /// 作业计划
        /// </summary>
        [DependencyProperty]
        public string ExecutionPlanName { get; set; }
        #endregion

        #region 描述
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 质量检查明细列表
        /// <summary>
        /// 质量检查明细列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<QualityInspectionDetailInfo> DetailParams { get; set; }
        #endregion

        #region 成品质检
        /// <summary>
        /// 成品质检
        /// </summary>
        [DependencyProperty]
        public ExecutionPlanInspectionInfo _executionPlanInspectionInfo { get; set; }
        #endregion

        #region 成品质检明细
        /// <summary>
        /// 成品质检明细
        /// </summary>
        [DependencyProperty]
        public QualityInspectionDetailInfo _qualityInspectionDetailInfo { get; set; }
        #endregion

        #region 审核状态列表
        /// <summary>
        /// 审核状态列表
        /// </summary>
        [DependencyProperty]
        public IDictionary<bool, string> ExamineTypes { get; set; }
        #endregion

        #region 审核意见
        /// <summary>
        /// 审核意见
        /// </summary>
        [DependencyProperty]
        public string CheckedRemark { get; set; }
        #endregion

        #region 已选审核状态
        /// <summary>
        /// 已选审核状态
        /// </summary>
        [DependencyProperty]
        public bool SelectedExamineType { get; set; }
        #endregion
        #endregion

        #region 方法
        #region 加载 —— async Task Load(WorkOrderInfo workOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(ExecutionPlanInspectionInfo executionPlanInspectionInfo)
        {
            this.ExamineTypes = new Dictionary<bool, string>();
            this.ExamineTypes.Add(true, "审核通过");
            this.ExamineTypes.Add(false, "审核不通过");
            this._executionPlanInspectionInfo = executionPlanInspectionInfo;
            this.InspectionNo = executionPlanInspectionInfo.Number;
            this.InspectionName = executionPlanInspectionInfo.Name;
            this.InspectedDate = executionPlanInspectionInfo.InspectedDate;
            this.ExecutionPlanName = executionPlanInspectionInfo.ExecutionPlanInfo.Name;
            this.Description = executionPlanInspectionInfo.Description;
            this.DetailParams = new ObservableCollection<QualityInspectionDetailInfo>(await Task.Run(() => this._iQualityContract.Channel.GetQualityInspectionDetails(
                executionPlanInspectionInfo.Id)));
            this._qualityInspectionDetailInfo = this.DetailParams.FirstOrDefault();
        }
        #endregion


        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证


            #endregion
            this.Busy();
            await Task.Run(() => this._iQualityContract.Channel.CheckQualityInspection(this._executionPlanInspectionInfo.Id, this.SelectedExamineType, this.CheckedRemark));
            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        //Private
        #endregion
    }
}
